package com.sdry.web.controller.hpf;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.sdry.model.hpf.Page;
import com.sdry.model.hpf.Reference;
import com.sdry.service.hpf.ReferenceService;
import com.sdry.utils.ResponseUtil;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import net.sf.json.JsonConfig;

/** 
* 作业指导书Controller
* @author lian 
* @version 1.0
* @time 2018年10月11日 下午5:14:31
*/

@Controller
@RequestMapping("/refrenceMana")
public class RefrenceController {
	
	@Resource
	private ReferenceService referenceService;  //作业指导书业务层
	
	/**
	 * 分页查询作业指导书
	 * @param page 分页条件
	 * @param refrence 查询条件
	 * @param response
	 * @throws Exception 
	 */
	@RequestMapping(value="/listPageReference")
	public void listPageRefrence(Page page, Reference reference, HttpServletResponse response) throws Exception{
		Map<String, Object> map = new HashMap<>();
		map.put("limit", page.getLimit());
		map.put("page", page.getPage());
		if (null !=  reference.getProductNumber() && !"".equals(reference.getProductNumber())) {
			map.put("productNumber", reference.getProductNumber());
		} else {
			map.put("productNumber", "");
			reference.setProductNumber("");
		}
		int count = referenceService.countReference(reference);
		List<Reference> productList = referenceService.listPageReference(map);
		JSONObject jsonObject = new JSONObject();
		JsonConfig jsonConfig = new JsonConfig();
		JSONArray referenceList = JSONArray.fromObject(productList, jsonConfig);
		jsonObject.put("code", 0);
		jsonObject.put("data", referenceList);
		jsonObject.put("count", count);
		ResponseUtil.write(response, jsonObject);
	}
	
	/**
	 * 保存作业指导书
	 * @param refrence 作业指导书实体类
	 * @param response
	 * @throws Exception 
	 */
	@RequestMapping(value="/saveReference")
	public void saveRefrence(Reference reference, HttpServletResponse response, HttpSession session) throws Exception{
		int isSuccess = 0;
		if (reference.getRid() == null || "".equals(reference.getRid())) {
			isSuccess = referenceService.insertReference(reference, session);
		} else {
			isSuccess = referenceService.updateReference(reference);
		}
		ResponseUtil.write(response, isSuccess);
	}
	
	/**
	 * 根据ID查询工艺详情
	 * @param response
	 * @throws Exception 
	 */
	@RequestMapping(value="/getReferenceById")
	public void getReferenceById(Reference reference, HttpServletResponse response, HttpSession session) throws Exception{
		if (reference.getPositionId() != null && !"".equals(reference.getPositionId())) {
			Reference re = referenceService.getReferenceById(reference.getPositionId());
			if(null != re.getRmethod() && "".equals(re.getRmethod())){
				ResponseUtil.write(response, re.getRmethod());
			}else{
				ResponseUtil.write(response, "无数据");
			}
		}
	}
	
	/**
	 * 删除作业指导书
	 * @param refrence 作业指导书实体类
	 * @param response
	 * @throws Exception 
	 */
	@RequestMapping(value="/deleteReference")
	public void deleteRefrence(@RequestParam("str") String str, HttpServletResponse response) throws Exception{
		int isSuccess = referenceService.deleteReference(str);
		ResponseUtil.write(response, isSuccess);
	}
	
	/**
	 * 根据名称和版本号查询工艺详情
	 * @param response
	 * @throws Exception 
	 */
	@RequestMapping(value="/getReferenceByNameAndNum")
	public void getReferenceByNameAndNum(Reference reference, HttpServletResponse response, HttpSession session) throws Exception{
		if (reference.getProductNumber() != null && !"".equals(reference.getProductNumber()) && reference.getEngineeringEnum() != null && !"".equals(reference.getEngineeringEnum()) ) {
			Reference re = referenceService.getReferenceByNameAndNum(reference);
			if(null != re){
				if(null != re.getRmethod() && !"".equals(re.getRmethod())){
					ResponseUtil.write(response, re.getRmethod());
				}else{
					ResponseUtil.write(response, "无数据");
				}
			}else{
				ResponseUtil.write(response, "无数据");
			}
			
		}
	}
}
